package com.lanou3g.musicenjoy.fragment;

import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.SingerInfor;
import com.lanou3g.musicenjoy.volley.VolleySingleton;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class InforFragment extends Fragment implements View.OnClickListener {
    private MusicFragment musicFragment;
    private SpecialFragment specialFragment;
    private Toolbar toolbar;
    private CollapsingToolbarLayout layout;
    private RequestQueue requestQueue;
    private SingerInfor singerInfor;
    private ImageView header;
    private ImageView backInfor;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_infor,null);
        musicFragment=new MusicFragment();
        specialFragment=new SpecialFragment();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        backInfor= (ImageView) getActivity().findViewById(R.id.backInfor);
        backInfor.setOnClickListener(this);
        header= (ImageView) getActivity().findViewById(R.id.header);
        requestQueue= VolleySingleton.getInstance(getActivity()).getRequestQueue();
        setToolbar();
        setCollapsingToolbarLayout();
        setViewPager();
        Bundle bundle1 = getArguments();
        Bundle bundle = new Bundle();
        String a = bundle1.getString("a");
        bundle.putString("aa", a);
        musicFragment.setArguments(bundle);
        specialFragment.setArguments(bundle);
        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0" +
                "&method=baidu.ting.artist.getinfo&format=json&tinguid=" + a + "&artistid=" + a;
        Log.i("xinxin", url + "");
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    singerInfor = gson.fromJson(response, SingerInfor.class);
                    if (singerInfor != null) {
                        Picasso.with(getActivity()).load(singerInfor.getAvatar_s1000()).into(header);
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(request);

    }

    private void setViewPager() {
        final ViewPager viewPager= (ViewPager) getActivity().findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        TabLayout tabLayout= (TabLayout) getActivity().findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

    }

    private void setCollapsingToolbarLayout() {
        layout= (CollapsingToolbarLayout) getActivity().findViewById(R.id.collapse_toolbar);
        layout.setTitleEnabled(false);
    }

    private void setToolbar() {
        toolbar= (Toolbar) getActivity().findViewById(R.id.toolbar);

    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
        adapter.addFragment(musicFragment, "歌曲");
        adapter.addFragment(specialFragment, "专辑");
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
       getFragmentManager().beginTransaction().remove(musicFragment).commit();
        getFragmentManager().beginTransaction().remove(specialFragment).commit();
    }

    static class ViewPagerAdapter extends FragmentPagerAdapter{
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
        public void addFragment(Fragment fragment,String s){
            mFragmentList.add(fragment);
            mFragmentTitleList.add(s);
        }

    }
}
